package com.wang.gulimall.product.service.impl;

import com.mysql.cj.util.StringUtils;
import com.wang.gulimall.product.entity.AttrEntity;
import com.wang.gulimall.product.service.AttrService;
import com.wang.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.wang.gulimall.product.vo.SpuItemAttrGroupVo;
import org.hibernate.validator.internal.engine.messageinterpolation.parser.ELState;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.utils.PageUtils;
import com.wang.common.utils.Query;

import com.wang.gulimall.product.dao.AttrGroupDao;
import com.wang.gulimall.product.entity.AttrGroupEntity;
import com.wang.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        //3级分类id为0的话查询所有数据
        if (catelogId == null || catelogId ==0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper.and(!StringUtils.isEmptyOrWhitespaceOnly((String) params.get("key")),
                    (obj) -> {
                        String key = (String) params.get("key");
                        obj.eq("attr_group_id", key).or().like("attr_group_name", key);
                    }
            ));
            return new PageUtils(page);
        }else {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper.eq("catelog_id", catelogId).and(!StringUtils.isEmptyOrWhitespaceOnly((String) params.get("key")),
                    (obj) -> {
                        String key = (String) params.get("key");
                        obj.eq("attr_group_id", key).or().like("attr_group_name", key);
                    }
            ));
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //com.atguigu.gulimall.product.vo
        //1、查询分组信息 查询groupID
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());//查询groupID对应的属性
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        //AttrGroupWithAttrsVo(attrGroupId=2, attrGroupName=智能手机, sort=2, descript=智能手机, icon=1, catelogId=225,
        // attrs=[AttrEntity(attrId=2, attrName=入网型号, searchType=0, icon=network, valueSelect=全网通, attrType=1, enable=1, catelogId=225, showDesc=0), AttrEntity(attrId=3, attrName=上市年份, searchType=0, icon=111, valueSelect=2020, attrType=1, enable=1, catelogId=225, showDesc=0), AttrEntity(attrId=4, attrName=机身颜色, searchType=0, icon=111, valueSelect=白色, attrType=1, enable=1, catelogId=225, showDesc=0), AttrEntity(attrId=5, attrName=手机屏幕, searchType=0, icon=size, valueSelect=6.0英寸, attrType=1, enable=1, catelogId=225, showDesc=0)])
        return collect;

    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return vos;
    }


}